package wowraidmanager.client.wishlist.admin;

import java.util.ArrayList;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.RaidbossDroplistService;
import wowraidmanager.client.service.wishlist.RaidbossDroplistServiceAsync;
import wowraidmanager.client.wishlist.utils.CheckError;
import wowraidmanager.shared.model.wishlist.Droplist;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ImportlistBossInfo extends BaseComposite{

    private static ImportlistBossInfoUiBinder uiBinder = GWT.create(ImportlistBossInfoUiBinder.class);

    protected RaidbossDroplistServiceAsync raidbossDroplistService = GWT.create(RaidbossDroplistService.class);

    interface ImportlistBossInfoUiBinder extends UiBinder<Widget, ImportlistBossInfo>{
    }

    ArrayList<RaidbossDroplist> raidbossdroplist;

    String raidzone;

    static int COLUMN_BOSSNAME = 0;
    static int COLUMN_BOSSID = 1;
    static int COLUMN_RAIDZONE = 2;
    static int COLUMN_GAMEVERSION = 3;
    static int COLUMN_ORDER = 4;
    static int COLUMN_DROPLIST = 5;
    static int COLUMN_HIDDENKEY = 6;

    static int COLUMN_DELETECHECK = 0;
    static int COLUMN_ITEMLINK = 1;
    static int COLUMN_ITEMID = 2;
    static int COLUMN_ITEMNAME = 3;
    static int COLUMN_ITEMRAIDZONE = 4;
    static int COLUMN_ITEMBOSSNAME = 5;
    static int COLUMN_ITEMCOLOR = 6;
    static int COLUMN_ISHEROIC = 7;
    static int COLUMN_ITEMHIDDENKEY = 8;


    @UiField
    FlexTable bossInfo;

    @UiField
    FlexTable bossDropitem;

    @UiField
    Button saveBossintoButton;

    @UiField
    Button saveItemListButton;

    @UiField
    Button addItemListButton;

    @UiField
    Label itemList;

    public ImportlistBossInfo(String raidzone, ArrayList<RaidbossDroplist> raidbossdroplist){
        initWidget(uiBinder.createAndBindUi(this));
        this.raidzone = raidzone;
        this.raidbossdroplist = raidbossdroplist;

        setBossInfo();
        setButton();
    }

    private void setButton(){
        saveBossintoButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                saveBossInfo();
            }
        });

        saveItemListButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                saveItemlist();
            }
        });

        addItemListButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                addNewItem();
            }
        });
    }

    protected void saveBossInfo(){
        for(int row = 1;row < bossInfo.getRowCount();row++){
            TextBox getValueTextBox = (TextBox)bossInfo.getWidget(row, COLUMN_BOSSNAME);
            String bossName = getValueTextBox.getText();

            getValueTextBox = (TextBox)bossInfo.getWidget(row, COLUMN_BOSSID);
            String bossId = getValueTextBox.getText();

            getValueTextBox = (TextBox)bossInfo.getWidget(row, COLUMN_RAIDZONE);
            String bossRaidzone = getValueTextBox.getText();

            getValueTextBox = (TextBox)bossInfo.getWidget(row, COLUMN_GAMEVERSION);
            String bossGameversion = getValueTextBox.getText();

            getValueTextBox = (TextBox)bossInfo.getWidget(row, COLUMN_ORDER);
            String bossOrder = getValueTextBox.getText();

            Hidden getValueHidden = (Hidden)bossInfo.getWidget(row, COLUMN_HIDDENKEY);
            String hiddenkey = getValueHidden.getValue();

            errorString = CheckError.checkBossInfo(bossName, bossId, bossRaidzone, bossGameversion, bossOrder);

            if(errorString.equals("")){
                for(RaidbossDroplist raidbossDroplist:raidbossdroplist){
                    if(raidbossDroplist.getKey().toString().equals(hiddenkey)){
                        raidbossDroplist.setBossName(bossName);
                        raidbossDroplist.setBossId(bossId);
                        raidbossDroplist.setRaidzone(bossRaidzone);
                        raidbossDroplist.setGameVersion(bossGameversion);
                        raidbossDroplist.setOrder(Integer.valueOf(bossOrder));
                    }
                }
            }else{
                break;
            }
        }
        if(errorString.equals("")){
            setRaidbossDroplistList();
        }else{
            clearInfoMessage();
            setErrorMessage(errorString);
        }
    }

    private void setRaidbossDroplistList(){
        raidbossDroplistService.setRaidbossDroplistList(raidbossdroplist, new AsyncCallback<Void>(){

            public void onSuccess(Void result){
                setInfoMessage("BossInfoの更新に成功しました。");
                clearErrorMessage();
                setBossInfo();
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("BossInfoの更新に失敗しました。");
            }
        });
    }

    protected void saveItemlist(){
        Hidden hiddenKey = (Hidden)bossDropitem.getWidget(0, COLUMN_ITEMHIDDENKEY);
        String key = hiddenKey.getValue();
        ArrayList<Droplist> newDroplist = new ArrayList<Droplist>();
        for(int row = 1;row < bossDropitem.getRowCount();row++){
            CheckBox deleteCheckbox = (CheckBox)bossDropitem.getWidget(row, COLUMN_DELETECHECK);

            if(!deleteCheckbox.getValue()){
                TextBox getValueTextBox = (TextBox)bossDropitem.getWidget(row, COLUMN_ITEMID);
                String itemId =getValueTextBox.getText();

                getValueTextBox = (TextBox)bossDropitem.getWidget(row, COLUMN_ITEMNAME);
                String itemName =getValueTextBox.getText();

                getValueTextBox = (TextBox)bossDropitem.getWidget(row, COLUMN_ITEMRAIDZONE);
                String itemRaidzone =getValueTextBox.getText();

                getValueTextBox = (TextBox)bossDropitem.getWidget(row, COLUMN_ITEMBOSSNAME);
                String itemBossname =getValueTextBox.getText();

                getValueTextBox = (TextBox)bossDropitem.getWidget(row, COLUMN_ITEMCOLOR);
                String itemColor =getValueTextBox.getText();

                CheckBox getValueCheckBox = (CheckBox)bossDropitem.getWidget(row, COLUMN_ISHEROIC);
                boolean itemIsHeroic =getValueCheckBox.getValue();

                errorString = CheckError.checkItemlist(itemId, itemName, itemRaidzone, itemBossname, itemColor, itemIsHeroic);

                if(errorString.equals("")){
                    Droplist newItem = new Droplist();
                    newItem.setId(itemId);
                    newItem.setItemName(itemName);
                    newItem.setRaidzone(itemRaidzone);
                    newItem.setBossName(itemBossname);
                    newItem.setItemColor(itemColor);
                    newItem.setisHeroic(itemIsHeroic);

                    newDroplist.add(newItem);
                }else{
                    break;
                }
            }
        }

        if(errorString.equals("")){
            for(RaidbossDroplist droplist:raidbossdroplist){
                if(droplist.getKey().toString().equals(key)){
                    droplist.setDroplist(newDroplist);
                    setRaidbossItemlist(droplist);
                    break;
                }
            }
        }else{
            clearInfoMessage();
            setErrorMessage(errorString);
        }
    }

    private void setRaidbossItemlist(final RaidbossDroplist raidbossdroplist){
        raidbossDroplistService.setRaidbossDroplist(raidbossdroplist, new AsyncCallback<Void>(){

            public void onSuccess(Void result){
                setInfoMessage("ItemListの更新に成功しました。");
                clearErrorMessage();
                setDropItem(raidbossdroplist.getDroplist(),raidbossdroplist.getKey().toString());
            }

            public void onFailure(Throwable caught){
                clearInfoMessage();
                setErrorMessage("ItemListの更新に失敗しました。");
            }
        });
    }

    protected void addNewItem(){
        int row = bossDropitem.getRowCount();

        bossDropitem.setWidget(row, 0, new CheckBox());
        bossDropitem.getCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);

        TextBox itemId = new TextBox();
        bossDropitem.setWidget(row, COLUMN_ITEMID, itemId);

        TextBox itemName = new TextBox();
        bossDropitem.setWidget(row, COLUMN_ITEMNAME, itemName);

        TextBox itemRaidzone = new TextBox();
        bossDropitem.setWidget(row, COLUMN_ITEMRAIDZONE, itemRaidzone);

        TextBox itemBossName = new TextBox();
        bossDropitem.setWidget(row, COLUMN_ITEMBOSSNAME, itemBossName);

        TextBox itemColor = new TextBox();
        bossDropitem.setWidget(row, COLUMN_ITEMCOLOR, itemColor);

        CheckBox itemHeroic = new CheckBox();
        bossDropitem.setWidget(row, COLUMN_ISHEROIC, itemHeroic);
        bossDropitem.getCellFormatter().setHorizontalAlignment(row, COLUMN_ISHEROIC, HasHorizontalAlignment.ALIGN_CENTER);
    }

    protected void setBossInfo(){
        bossInfo.removeAllRows();
        bossInfo.setText(0, COLUMN_BOSSNAME, "BossName");
        bossInfo.setText(0, COLUMN_BOSSID, "BossId");
        bossInfo.setText(0, COLUMN_RAIDZONE, "RaidZone");
        bossInfo.setText(0, COLUMN_GAMEVERSION, "GameVersion");
        bossInfo.setText(0, COLUMN_ORDER, "Order");
        bossInfo.setText(0, COLUMN_DROPLIST, "DropList");

        int row = 1;
        for(RaidbossDroplist droplist:raidbossdroplist){
            TextBox bossName = new TextBox();
            bossName.setText(droplist.getBossName());
            bossInfo.setWidget(row, COLUMN_BOSSNAME, bossName);

            TextBox bossId = new TextBox();
            bossId.setText(droplist.getBossId());
            bossInfo.setWidget(row, COLUMN_BOSSID, bossId);

            TextBox raidZone = new TextBox();
            raidZone.setText(droplist.getRaidzone());
            bossInfo.setWidget(row, COLUMN_RAIDZONE, raidZone);

            TextBox gameVersion = new TextBox();
            gameVersion.setText(droplist.getGameVersion());
            bossInfo.setWidget(row, COLUMN_GAMEVERSION, gameVersion);

            TextBox order = new TextBox();
            order.setText(String.valueOf(droplist.getOrder()));
            bossInfo.setWidget(row, COLUMN_ORDER, order);

            Button dropList = new Button();
            dropList.setText("View");
            bossInfo.setWidget(row, COLUMN_DROPLIST, dropList);
            setButtonEvent(dropList, droplist);

            Hidden key = new Hidden();
            key.setValue(droplist.getKey().toString());
            bossInfo.setWidget(row, COLUMN_HIDDENKEY, key);

            row++;
        }

    }

    private void setButtonEvent(Button dropList, final RaidbossDroplist raidbossdroplist){
        dropList.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setDropItem(raidbossdroplist.getDroplist(), raidbossdroplist.getKey().toString());
                itemList.setText("ItemList(" + raidbossdroplist.getBossName() + ")");
                itemList.setVisible(true);
                saveItemListButton.setVisible(true);
                addItemListButton.setVisible(true);
            }
        });
    }

    protected void setDropItem(ArrayList<Droplist> droplist, String key){
        bossDropitem.removeAllRows();
        int row = 0;

        bossDropitem.setText(row, COLUMN_DELETECHECK, "DELTE");
        bossDropitem.setText(row, COLUMN_ITEMLINK, "ItemLink");
        bossDropitem.setText(row, COLUMN_ITEMID, "ItemID");
        bossDropitem.setText(row, COLUMN_ITEMNAME, "ItemName");
        bossDropitem.setText(row, COLUMN_ITEMRAIDZONE, "Raidzone");
        bossDropitem.setText(row, COLUMN_ITEMBOSSNAME, "BossName");
        bossDropitem.setText(row, COLUMN_ITEMCOLOR, "ItemColor");
        bossDropitem.setText(row, COLUMN_ISHEROIC, "IsHeroic");
        bossDropitem.setWidget(row, COLUMN_ITEMHIDDENKEY, new Hidden("raidbossKey", key));

        row++;
        row = setItemList(droplist, row, false);
        setItemList(droplist, row, true);

    }

    private int setItemList(ArrayList<Droplist> droplist, int row, boolean isHeroic){

        for(Droplist item:droplist){
            if(item.isHeroic() == isHeroic){
                bossDropitem.setWidget(row, 0, new CheckBox());
                bossDropitem.getCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);

                bossDropitem.setHTML(row, 1, "<a href=\"http://www.wowhead.com/item=" + item.getId() + "\" rel=\"item=" + item.getId() + "\" class=\"" + item.getItemColor() + "\" target=\"_blank\">[" + item.getItemName() + "]</a>");

                TextBox itemId = new TextBox();
                itemId.setText(item.getId());
                bossDropitem.setWidget(row, COLUMN_ITEMID, itemId);

                TextBox itemName = new TextBox();
                itemName.setText(item.getItemName());
                bossDropitem.setWidget(row, COLUMN_ITEMNAME, itemName);

                TextBox itemRaidzone = new TextBox();
                itemRaidzone.setText(item.getRaidzone());
                bossDropitem.setWidget(row, COLUMN_ITEMRAIDZONE, itemRaidzone);

                TextBox itemBossName = new TextBox();
                itemBossName.setText(item.getBossName());
                bossDropitem.setWidget(row, COLUMN_ITEMBOSSNAME, itemBossName);

                TextBox itemColor = new TextBox();
                itemColor.setText(item.getItemColor());
                bossDropitem.setWidget(row, COLUMN_ITEMCOLOR, itemColor);

                CheckBox itemHeroic = new CheckBox();
                itemHeroic.setValue(item.isHeroic());
                bossDropitem.setWidget(row, COLUMN_ISHEROIC, itemHeroic);
                bossDropitem.getCellFormatter().setHorizontalAlignment(row, COLUMN_ISHEROIC, HasHorizontalAlignment.ALIGN_CENTER);

                row++;
            }
        }

        return row;
    }



}
